package org.example.platform.gaode.service;


import org.example.platform.gaode.dto.Lives;
import org.example.platform.gaode.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


/**
 * 对接高德天气
 */
@Service
public class RestTemplateToInterface {
    @Autowired
    private RestTemplate restTemplate;

    // 获取实时天气
    String URL = "https://restapi.amap.com/v3/weather/weatherInfo?key=a47fcf256e1e44b94b28ca0320bfa579&city=310105";

    // 获取实时天气
    String URL2 = "https://restapi.amap.com/v3/weather/weatherInfo?key=a47fcf256e1e44b94b28ca0320bfa579&city=310105";

    /**
     * 以get方式请求第三方http接口 getForEntity
     * @param url
     * @return
     */
    public Result doGetWith1(String url){
        ResponseEntity<Result> responseEntity = restTemplate.getForEntity(URL, Result.class);
        Result result = responseEntity.getBody();
        System.out.println(result.toString());
        return result;
    }

    /**
     * 以get方式请求第三方http接口，返回值是响应体，省去了getBody
     * @return
     */
    public Result doGetWith2(){
        return restTemplate.getForObject(URL,Result.class);
    }

    /**
     * 以post方式请求第三方http接口
     * @return
     */
    public String doPostWith1(){
        Lives lives = new Lives();
        lives.setCity("10105");
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(URL2,lives,String.class);

        return responseEntity.getBody();
    }


    /**
     * 以post方式请求第三方http接口
     * @return
     */
    public String doPostWith2(){
        Lives lives = new Lives();
        lives.setCity("10105");
        return restTemplate.postForObject(URL2,lives,String.class);
    }


}
